<?php
/*******************************************
* Orain 入口文件
* 初始化相关操作
******************************************/
error_reporting(E_ALL ^ E_NOTICE); 
if(phpversion() < 5){
	exit("PHP版本最低要求:5.0");
}
$GLOBALS['__STARTTIME__'] = microtime(TRUE);
define('IN_ORA','true');
//更新地址
define('ORA_UPDATE','http://www.lan7.net/index.php/');
//当前版本
define('ORA_VER','2.0');
//设置时区
date_default_timezone_set('PRC');
// 0 为调试模式, 1 为正式运行模式
$GLOBALS['RUN_MODEL'] = 1;
//程序路径
define('ROOT_PATH', str_replace("\\", "/", dirname(__FILE__)) . '/');
//程序Url
define('HTTP_HOST',array_shift(explode("/index.php","http://".$_SERVER["HTTP_HOST"].$_SERVER['SCRIPT_NAME'])));
//HTML文件目录
define('HTML_PATH',ROOT_PATH."html/");
//上传目录
define('UPLOAD_PATH',ROOT_PATH."upload/");
//公共文件目录
define('PUBLIC_PATH',ROOT_PATH."public/");
//缓存目录
define('CACHE_PATH',ROOT_PATH."cache/");
//加载配置文件
$GLOBALS["ora_cfg"] = include_once(ROOT_PATH."config/config.php");
if(isset($_GET['t'])){
	$GLOBALS['ora_cfg']['themes'] = $_GET['t'];
	$GLOBALS['ora_cfg']['app_type']=$_GET['t'];
}
//更新文件目录
define('UPDATE_PATH',ROOT_PATH."update/");
//模板路径
define('THEMES_PATH',ROOT_PATH."themes/");
//模板路径
define('PLUGIN_PATH',ROOT_PATH."plugin/");
//当前默认模板路径
define('TPL_PATH',ROOT_PATH."themes/".$GLOBALS["ora_cfg"]["themes"]."/");
//加载系统类库
if(!file_exists(ROOT_PATH.'cache/~runtime.php')){
	$s  = file_get_contents(ROOT_PATH."source/base.class.php");
	$s .= file_get_contents(ROOT_PATH."source/io.class.php");
	$s .= file_get_contents(ROOT_PATH."source/cache.class.php");
	$s .= file_get_contents(ROOT_PATH."source/tpl.class.php");
	$s .= file_get_contents(ROOT_PATH."source/".$GLOBALS["ora_cfg"]["db_driver"].".class.php");
	$s .= file_get_contents(ROOT_PATH."source/mod.class.php");
	$s .= file_get_contents(ROOT_PATH."source/plugin.class.php");
	$s .= file_get_contents(ROOT_PATH."common.php");
	if(!is_dir(CACHE_PATH)){
		mkdir(CACHE_PATH);
	}
	file_put_contents(ROOT_PATH.'cache/~runtime.php',$s);
	if($GLOBALS['RUN_MODEL'] === 1){
		file_put_contents(ROOT_PATH.'cache/~runtime.php',php_strip_whitespace(ROOT_PATH.'cache/~runtime.php'));
	}
}
include_once(ROOT_PATH.'cache/~runtime.php');
$GLOBALS["__QUERY__"] = 0;
//实例化模板操作对象
$GLOBALS["tpl"] = new tpl(ROOT_PATH."cache/template/".$GLOBALS["ora_cfg"]["themes"]."/",$GLOBALS["ora_cfg"]["cache_time"],$GLOBALS["ora_cfg"]["mk_html"],false);
//实例化数据库操作对象
$dbclsname =$GLOBALS["ora_cfg"]["db_driver"];
$GLOBALS['db'] = new $dbclsname;
//路由解析,URL参数重写
orain_router(intval($GLOBALS["ora_cfg"]["router"]));
//检测是否存在对应的可用html页面
if($GLOBALS["ora_cfg"]["mk_html"]&&!in_array($GLOBALS['mod'],$GLOBALS["ora_cfg"]["mod_array"])){
	if(!io::isExp(get_html_path(),$GLOBALS["ora_cfg"]["cache_time"])){
		redirect(get_html_url(),false);
	}
}
//请求转发控制
if(in_array($GLOBALS["mod"],$GLOBALS["ora_cfg"]['acs'])){
	if(is_file(ROOT_PATH."module/".$GLOBALS["mod"].".mod.php")){
		if(!get_magic_quotes_gpc()){
			if(!empty($_GET)) $_GET     = addslashes_deep($_GET);
			if(!empty($_POST)) $_POST    = addslashes_deep($_POST);
		}
		include_once(ROOT_PATH."module/".$GLOBALS["mod"].".mod.php");
		$tmp = new $GLOBALS["mod"]();
		if(method_exists($tmp,$GLOBALS['act'])){
			$tmp->$GLOBALS["act"]();
		}else{
			$tmp->setTplDir(PUBLIC_PATH);
			$tmp->setMsgTpl("admin/msg.html");
			$tmp->showMsg("该页面不存在!");
		}
	}else{
		throwException("模块 ".$GLOBALS["mod"]." 未找到! ",__file__,__line__);
	}
}else{
	throwException("未授权的请求!",__file__,__line__);
}
?>